package com.gjy.redis.controller;

import com.gjy.redis.domain.AjaxResult;
import org.springframework.data.redis.core.HyperLogLogOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 宫静雨
 * @version 1.0
 * @since 2023-07-13 11:35:03
 */
@RestController
@RequestMapping("/pf")
public class PfController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/add")
    public AjaxResult add(@RequestParam Integer end) {
        HyperLogLogOperations<String, Object> ops = redisTemplate.opsForHyperLogLog();
        /*Integer[] integers = new Integer[end];
        for (int i = 0; i < end; i++) {
            integers[i] = i;
        }
        ops.add("PF_1", integers);*/
        Integer[] integers = new Integer[1000000];
        for (int j = 0; j < 10; j++) {
            for (int i = 0; i < integers.length; i++) {
                integers[i] = i + (j * 1000000);
            }
            ops.add("PF_1", integers);
        }


        return AjaxResult.builder().build();
    }

    @PostMapping("/count")
    public AjaxResult count() {
        HyperLogLogOperations<String, Object> ops = redisTemplate.opsForHyperLogLog();
        Long pf_1 = ops.size("PF_1");
        return AjaxResult.builder().data(pf_1).build();
    }
}
